package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.ClassifyChild;
import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.domain.ProductClassify;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.serivce.IClassifyChildService;
import cn.wolfcode.crm.serivce.IProductClassifyService;
import cn.wolfcode.crm.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("productClassify")
public class ProductClassifyController {
    @Autowired
    private IProductClassifyService service;
    @Autowired
    private IClassifyChildService classifyChildService;

    @RequestMapping("view")
    public String view() throws Exception {
        return "productClassify";
    }

    @RequestMapping("listAll")
    @ResponseBody
    public List<ProductClassify> listAll(QueryObject qo) throws Exception {
        List<ProductClassify> productClassifys = service.selectAll();
        return productClassifys;
    }
    @RequestMapping("selectClassify")
    @ResponseBody
    public ClassifyChild selectClassify(Integer classNumber)throws Exception{
        ClassifyChild classifyChildren = service.selectClassify(classNumber);
        return classifyChildren;
    }
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id) throws Exception {
        try {
            ProductClassify productClassify = service.selectByPrimaryKey(id);
            service.deleteByPrimaryKey(id);
            //classifyChildService.deleteByclassNumber(productClassify.getClassNumber());
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("删除失败!");
        }
    }
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(ProductClassify pcy,ClassifyChild c) throws Exception {
        try {
            service.saveOrUpdate(pcy);
            classifyChildService.saveOrUpdate(c);
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult().mark("保存失败!");
        }
    }


}
